VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Factory"
Attribute VB_GlobalNameSpace = True
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True

'
'Copyright (c) 2010 Andrew Wall
'
' http://www.opensource.org/licenses/mit-license.php
'
'Permission is hereby granted, free of charge, to any person obtaining a copy
'of this software and associated documentation files (the "Software"), to deal
'in the Software without restriction, including without limitation the rights
'to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the Software is
'furnished to do so, subject to the following conditions:
'
'The above copyright notice and this permission notice shall be included in
'all copies or substantial portions of the Software.
'
'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
'THE SOFTWARE.
'

Option Explicit

Private encodings As New Dictionary

Public Function NewDocument(name As String) As Document
Dim D As New XMLDocument
D.init name
Set NewDocument = D
End Function

Friend Function NewText(TextValue As String) As Text
Dim T As New XMLText
T.TextValue = EncodeXMLChars(TextValue)
Set NewText = T
End Function
Friend Function EncodeXMLChars(value As String) As String
EncodeXMLChars = EncodingLoop(value)
'EncodeXMLChars = EncodingBuffer(value)
'EncodeXMLChars = EncodeXML(value)
End Function

Friend Function EncodingLoop(ByVal value As String) As String
Dim key As Variant
For Each key In encodings
    value = Replace(value, key, encodings.Item(key))
Next
EncodingLoop = value
End Function

Friend Function EncodingBuffer(value As String) As String
Dim ret As String
Dim out As String
Dim outIndex As Integer
Dim outLen As Integer
Dim index As Integer
Dim inp As String
Dim vLen As Integer

vLen = Len(value)
ret = Space(vLen * 6)
outIndex = 1
For index = 1 To vLen
    inp = Mid(value, index, 1)
    If encodings.Exists(inp) Then
        out = encodings.Item(inp)
        outLen = Len(out)
        Mid(ret, outIndex, outLen) = out
    Else
        outLen = 1
        Mid(ret, outIndex) = inp
    End If
    outIndex = outIndex + outLen
Next
EncodingBuffer = Left(ret, outIndex - 1)
End Function

Private Sub Class_Initialize()
'Note the & -> &amp; encoding MUST be first!
encodings.Add "&", "&amp;"
encodings.Add "<", "&lt;"
encodings.Add ">", "&gt;"
encodings.Add "'", "&apos;"
encodings.Add """", "&quot;"

End Sub
